package com.niit.qcx.companyPrice;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

/**数据中包含的字段
 * title comm  total_price unit_price  no_room  area  orientations  floor  year  address	county	town  tags  broker	rate  company
 *Mapper阶段：
 * 提取房产公司名称（company）和价格（单价unit_price）
 * 去除单价字段里的“元/㎡”，只留下数值方便计算
 * 将房产公司名称作为键，价格作为值输出，例如，输出<正顺地产,6000元/㎡>
 */

public class compPMapper extends Mapper<LongWritable, Text, Text, FloatWritable> {
    private Text outk = new Text();
    private FloatWritable outv = new FloatWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();
        String[] fields = line.split("\t");

        if (key.get() == 0) {// 跳过第一行标题
            return;
        }

        if (fields.length >= 4) {// 确保有足够的字段

            String unit_priceStr = fields[3];
            float unit_price = Float.parseFloat(unit_priceStr.replace("元/㎡",""));

            outk.set(fields[fields.length-1]);  // 公司名称在第16个位置，倒数第一个
            outv.set(unit_price);
            context.write(outk,outv);
        }

    }
}

